package com.lulin.mall.search.controller;

import com.lulin.common.dto.es.SkuEsModel;
import com.lulin.common.exception.BizCodEnum;
import com.lulin.common.utils.R;
import com.lulin.mall.search.service.ElasticSearchSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * 存储商品上架的数据到ElasticSearch服务
 *
 * @author lulin
 * @date 2022/11/11 17:56
 */
@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSearchSaveController {

    @Autowired
    private ElasticSearchSaveService searchSaveService;

    /**
     * 存储商品数据到ElasticSearch服务
     *
     * @param skuEsModels
     * @return
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        Boolean b = false;
        //商品上架数据保存到es服务器
        try {
            b = searchSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            //e.printStackTrace();
            log.error("ElasticSearch商品上架错误：{}", e);
            //商品上架异常
            return R.error(BizCodEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (b) {
            return R.ok();
        }
        return R.error(BizCodEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodEnum.PRODUCT_UP_EXCEPTION.getMsg());
    }
}
